<?php defined("MAIN_DIR") or die("No direct access");

if ($route->POST("action") == "post"){

	$quote_id = UV::rInt($route->POST("id"));
    $text = UV::rStr($route->POST("text"));
    $book_id = UV::rInt($route->POST("book"));
    $status = UV::rStr($route->POST("status"));

	$query = "update `quotes` set `text`='$text',`book_id`='$book_id',`status`='$status' where `id` = '$quote_id' limit 1";
	if ($db->sendQuery($query) !== false){
		to("/admin/quotes/edit/{$quote_id}");
	} else {
		echo "Ошибка - ".mysql_error();
	}

} else {

	$query = "insert into `quotes` values(NULL,'{$user->id()}','','0','0','auto-draft')";
	$quote_id = $db->sendQuery($query);
}

require_once ADMIN.HEADFILE;
?>

<h1>Добавить цитату</h1>

<form method='post' id='form'>
	<input type='hidden' name='action' value='post' />
	<input type='hidden' name='id' value='<?php echo $quote_id; ?>' />
	<table class='edit'>
		<tr>
			<td colspan='2'>
				<textarea placeholder='Цитата' name='text' id='text'><?php echo $route->POST("text"); ?></textarea>
				<div class='error'>Необходимо заполнить «Цитату»</div>
			</td>
		</tr>
		<tr>
			<td class='label'><label for='book'>Книга</label></td>
			<td>
				<select name="book" id="book" class='medium'><?php
					$books = $db->getTable("select `id`,`name` from `books` where `status` = 'placed' order by `name`");
					echo selectCreate($books,"id","name",$route->POST("book"));
				?></select>
			</td>
		</tr>
		<tr>
			<td class='label'><label for='status'>Статус</label></td>
			<td>
				<input type='radio' name="status" value="placed" <?php echo ($route->POST("status") != "draft") ? "checked='checked'" : ""; ?> id="placed" />
				<label for='placed' class='small'>Размещено</label>
				<input type='radio' name="status" value="draft" <?php echo ($route->POST("status") == "draft") ? "checked='checked'" : ""; ?> id="draft" />
				<label for='draft' class='small'>В черновике</label>
			</td>
		</tr>
		<tr>
			<td class='label' colspan='2'>
				<input type='submit' value='Добавить цитату' class='button' />
			</td>
		</tr>
	</table>
</form>

<?php require_once ADMIN.FOOTERFILE; ?>